package com.ruoyi.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.convert.EnergyMeterConverter;
import com.ruoyi.management.mapper.EnergyMeterMapper;
import com.ruoyi.management.pojo.energymeter.EnergyMeterDO;
import com.ruoyi.management.pojo.energymeter.EnergyMeterDTO;
import com.ruoyi.management.pojo.energymeter.EnergyMeterVO;
import com.ruoyi.management.service.EnergyMeterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电表型号服务实现类
 */
@Slf4j
@Service
public class EnergyMeterServiceImpl implements EnergyMeterService {

    @Autowired
    private EnergyMeterMapper energyMeterMapper;

    @Autowired
    private EnergyMeterConverter energyMeterConverter;

    /**
     * 根据id查询电表型号
     *
     * @param modelId 电表型号id
     * @return 电表型号信息
     */
    @Override
    public EnergyMeterVO getEnergyMeterById(Long modelId) {
        try {
            EnergyMeterDO energyMeterDO = energyMeterMapper.selectById(modelId);
            return energyMeterConverter.toVO(energyMeterDO);
        } catch (Exception e) {
            log.error("查询电表型号异常: {}", e.toString());
            return null;
        }
    }

    /**
     * 查询所有电表型号
     *
     * @param energyMeterDTO 查询参数
     * @return 电表型号列表
     */
    @Override
    public List<EnergyMeterVO> getEnergyMeterList(EnergyMeterDTO energyMeterDTO) {
        try {
            QueryWrapper<EnergyMeterDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .like(StringUtils.isNotBlank(energyMeterDTO.getModelName()), EnergyMeterDO::getModelName, energyMeterDTO.getModelName())
                    .orderByAsc(EnergyMeterDO::getModelId)
            ;
            return energyMeterConverter.toVOList(energyMeterMapper.selectList(queryWrapper));
        } catch (Exception e) {
            log.error("查询电表型号异常: {}", e.toString());
            return null;
        }
    }

    /**
     * 分页查询所有电表型号
     *
     * @param energyMeterDTO 查询参数
     * @return 电表型号列表
     */
    @Override
    public Page<EnergyMeterVO> getEnergyMeterPageList(EnergyMeterDTO energyMeterDTO) {

        try {
            QueryWrapper<EnergyMeterDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .like(StringUtils.isNotBlank(energyMeterDTO.getModelName()), EnergyMeterDO::getModelName, energyMeterDTO.getModelName())
                    .orderByAsc(EnergyMeterDO::getModelId)
            ;
            Page<EnergyMeterDO> page = new Page<>(energyMeterDTO.getPageNum() == null ? 1 : energyMeterDTO.getPageNum(),
                    energyMeterDTO.getPageSize() == null ? 10 : energyMeterDTO.getPageSize());

            return energyMeterConverter.toPageVOList(energyMeterMapper.selectPage(page, queryWrapper));
        } catch (Exception e) {
            log.error("查询电表型号异常: {}", e.toString());
            return null;
        }
    }

    /**
     * 添加电表型号
     *
     * @param energyMeterDTO 电表型号信息
     * @return 添加结果
     */
    @Override
    public EnergyMeterVO addEnergyMeter(EnergyMeterDTO energyMeterDTO) {
        try {
            EnergyMeterDO aDo = energyMeterConverter.toDO(energyMeterDTO);
            int insert = energyMeterMapper.insert(aDo);
            if (insert > 0) {
                return energyMeterConverter.toVO(aDo);
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("添加电表型号异常: {}", e.toString());
            return null;
        }
    }

    /**
     * 修改电表型号
     *
     * @param energyMeterDTO 电表型号信息
     * @return 修改结果
     */
    @Override
    public int updateEnergyMeter(EnergyMeterDTO energyMeterDTO) {
        try {
            return energyMeterMapper.updateById(energyMeterConverter.toDO(energyMeterDTO));
        } catch (Exception e) {
            log.error("修改电表型号异常: {}", e.toString());
            return 0;
        }
    }

    /**
     * 删除电表型号
     *
     * @param modelId 电表型号id
     * @return 删除结果
     */
    @Override
    public int deleteEnergyMeter(Long modelId) {
        try {
            return energyMeterMapper.deleteById(modelId);
        } catch (Exception e) {
            log.error("删除电表型号异常: {}", e.toString());
            return 0;
        }
    }

}
